// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

export component TestCase inherits Rectangle {
    width: 400phx;
    height: 400phx;
    forward-focus: focus-scope;

    focus-scope := FocusScope { }

    fs := FocusScope {
        focus-changed-event => {
            root.focus-changed-event-counter += 1;
            scope-focused = self.has-focus;
        }
    }

    public function set-focus() {
        fs.focus();
    }

    out property <bool> scope-focused;
    out property <int> focus-changed-event-counter: 0;
}

/*
```rust
use slint::platform::Key;

let instance = TestCase::new().unwrap();
assert!(!instance.get_scope_focused());
assert_eq!(instance.get_focus_changed_event_counter(), 0);
slint_testing::send_keyboard_string_sequence(&instance, "\t");
assert!(instance.get_scope_focused());
assert_eq!(instance.get_focus_changed_event_counter(), 1);
slint_testing::send_keyboard_string_sequence(&instance, "\t");
assert!(!instance.get_scope_focused());
assert_eq!(instance.get_focus_changed_event_counter(), 2);
slint_testing::send_keyboard_string_sequence(&instance, "\t");
assert!(instance.get_scope_focused());
assert_eq!(instance.get_focus_changed_event_counter(), 3);

// alt-tab don't change focus
slint_testing::send_keyboard_char(&instance, Key::Alt.into(), true);
slint_testing::send_keyboard_string_sequence(&instance, "\t");
slint_testing::send_keyboard_char(&instance, Key::Alt.into(), false);
assert!(instance.get_scope_focused());
assert_eq!(instance.get_focus_changed_event_counter(), 3);

// calling `focus()` on focused item doesn't trigger change
instance.invoke_set_focus();
assert_eq!(instance.get_focus_changed_event_counter(), 3);
```

```cpp
auto handle = TestCase::create();
const TestCase &instance = *handle;
assert(!instance.get_scope_focused());
assert_eq(instance.get_focus_changed_event_counter(), 0);
slint_testing::send_keyboard_string_sequence(&instance, "\t");
assert(instance.get_scope_focused());
assert_eq(instance.get_focus_changed_event_counter(), 1);
slint_testing::send_keyboard_string_sequence(&instance, "\t");
assert(!instance.get_scope_focused());
assert_eq(instance.get_focus_changed_event_counter(), 2);
slint_testing::send_keyboard_string_sequence(&instance, "\t");
assert(instance.get_scope_focused());
assert_eq(instance.get_focus_changed_event_counter(), 3);

// calling `focus()` on focused item doesn't trigger change
instance.invoke_set_focus();
assert_eq(instance.get_focus_changed_event_counter(), 3);
```

```js
var instance = new slint.TestCase();
assert(!instance.scope_focused);
assert.equal(instance.focus_changed_event_counter, 0);
slintlib.private_api.send_keyboard_string_sequence(instance, "\t");
assert(instance.scope_focused);
assert.equal(instance.focus_changed_event_counter, 1);
slintlib.private_api.send_keyboard_string_sequence(instance, "\t");
assert(!instance.scope_focused);
assert.equal(instance.focus_changed_event_counter, 2);
slintlib.private_api.send_keyboard_string_sequence(instance, "\t");
assert(instance.scope_focused);
assert.equal(instance.focus_changed_event_counter, 3);

// calling `focus()` on focused item doesn't trigger change
instance.set_focus();
assert.equal(instance.focus_changed_event_counter, 3);
```
*/
